<?php

namespace App\Http\Controllers\Admin\Topic;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Topics;
use App\Models\Users;
use App\Models\Shares;
use Illuminate\Support\Facades\DB;
/**
 * 订单管理控制器
 *
 * @author yh
 * @package App\Http\Controllers\Orders
 */
class TopicsController extends BaseAdminController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 商家列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200318
     */
    public function index()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Topics::orderBy($sort, $order)->where('status', '<=', 2);
        if (request('title')) {
            $query->where('title', 'LIKE', "%" . request('title') . "%");
        }
        if(request('number')){
            $query->where('number','LIKE', "%" . request('number') . "%");
        }
        // 取得列表
        $shops = $query->select([
            'id',
            'joins',
            'times',
            'status',
            'num',
            'joins',
            'money',
            'title',
            'end_date',
            'start_date'
        ])->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $shops->appends(app('request')->all());

        return view('admin.topic.topics', [
            'topics' => $shops,
        ]);
    }
     /**
     * 添加主题
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20201104
     */
    public function add()
    {
        $cates= Shares::where('status', 1)->orderBy('id')->get()->toArray();
        return view('admin.topic.topics_form',[ 'cates' =>$cates ]);
    } 
    /**
	 * 编辑主题
	 *
	 * @author lxp 20170111
	 * @param $uid
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function edit($id)
	{
		// 取得用户信息
		$user = Topics::findOrFail($id);
        $cates= Shares::where('status', 1)->orderBy('id')->get()->toArray();
		return view('admin.topic.topics_form', [
            'user' => $user,
            'cates' =>$cates 
		]);
	}
     /**
     * 保存用户
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170206
     */
    public function save()
    {
       // 保存用户信息
		DB::transaction(function () {
            $user = Topics::findOrNew(request('id'));
            $rule= [];
            if (!request('id')) {
                $rule = array_merge([
                    'banners' => 'required|',
                    'content' => 'required|',
                    'infos' => 'required|',
                    'rules' => 'required|',
                    'shares' => 'required|',
                    
                ],$rule);
            }
            $this->validate(request(), $rule, [], [
                'banners' => '轮播图',
                'content' => '众筹详情',
                'infos'   => '用户须知',
                'rules'  => '参赛规则',
                'shares' =>'分销须知'
            ]);
			$user->title = request('title');
            $user->content = request('content');
            $user->infos = request('infos');
            $user->money = request('money');
            $user->num = request('num');
            $user->max_num= request('max_num');
            $user->banners= request('banners');
            $user->desc = request('desc');
            $user->start_date = request('start_date');
            $user->end_date = request('end_date');
            $user->rates1 = request('rates1');
            $user->rates2= request('rates2');
            $user->qrcode= request('qrcode');
            $user->rules = request('rules');
            $user->shares = request('shares');
            $user->share_id = request('share_id');
			$user ->timestamps = true;
			$user -> save();
			if (!request('id')) {
                // 更新店铺图片
                if (intval(request('file_id'))) {
                    UploadedFile::where('file_id', intval(request('file_id')))->update(['item_id' => $user->id]);
                }
            }
		});

		return $this->success(get_session_url('index'));
    }
    /**
     * 删除用户
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author lxp 20160713
     */
    public function delete($id)
    {
        if (request()->ajax() && intval($id)) {
            // 判断用户是否可以被删除
            // 删除用户
            Topics::destroy($id);
            return $this->success();
        }
    }
    }